<%@ page import="com.wang.utils.ConstantsUtil" %>
<%@ page import="com.wang.servlet.GetFileList" %>
<%@ page import="com.wang.utils.FileManager" %>
<%@ page import="javafx.util.Pair" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: wr
  Date: 2021/10/29
  Time: 17:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Home</title>
    <script src="https://unpkg.zhimg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<%
    String user = (String) request.getSession().getAttribute(ConstantsUtil.Login.USER_KEY);
%>
<h1>
    welcome <%=user%>
</h1>
<table border="1">
    <thead>
        <th>filename</th>
    </thead>
    <tbody id="fileList">

    </tbody>
</table>
<form action="/home/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>
<tr>
    <td><input type="button" value="刷新" onclick="refresh()"></td>
    <td><input type="button" value="退出" onclick="window.location.href='/logout'"></td>
</tr>

</body>
<script>
    refresh()
    function refresh() {
        axios.get("/home/getFileList").then(
            res => {
                console.log(res.data);
                let fileList = res.data;
                let fileTable = document.getElementById("fileList");
                fileTable.innerHTML = ''
                for(let index in fileList){
                    //str+="<tr><a href='/home/download?filename="+fileList[index]+"'>"+fileList[index]+"</a></tr>"
                    let tr = document.createElement("tr");
                    let a = document.createElement("a");
                    a.setAttribute("href","/home/download?filename="+fileList[index])
                    a.innerText=fileList[index]
                    tr.appendChild(a)
                    fileTable.appendChild(tr)
                }
            }
        )
    }
</script>
</html>
